//
//  MovieDetailViewController.m
//  WisdomZhuHai
//
//  Created by Ryan on 14-12-4.
//  Copyright (c) 2014年 wyd. All rights reserved.
//

#import "MovieDetailViewController.h"
#import "HotRateView.h"
#import "MovieViewController.h"
#import "GoodDetailViewController.h"
#import "SingleMovieDetailViewController.h"
#import "CinemaDetailViewController.h"

static const CGFloat TITLECELL_HEIGHT = 110;
static const CGFloat MOVIETICKETCELL_HEIGHT = 45;
static const CGFloat MIDDLEMOVIECELL_HEIGHT = 180;
static const CGFloat MOVIELISTCELL_HEIGHT = 180;
static const CGFloat MOVIESHOWTIME_HEIGHT = 69;
static const CGFloat MOVIENAMECELL = 45;


@interface MovieDetailViewController ()

@property (nonatomic,assign) int currentIndex;
@property (nonatomic,assign) int currentId;
@property (nonatomic,assign) int updateTime;
@property (nonatomic,assign) int detailId;

@property (nonatomic,strong) UIView* Today;
@property (nonatomic,strong) UIView* Tomorrow;
@property (nonatomic,strong) UIView* AfterTomorrow;

@property (nonatomic,strong) NSMutableArray* ticketList;
@property (nonatomic,strong) NSMutableArray* movieTimeList;
@property (nonatomic,strong) NSDictionary* cinemaInfo;
@property (nonatomic,strong) NSArray* movieInfo;
@property (nonatomic,strong) NSDictionary* movieDetail;
@property (nonatomic,strong) UIScrollView* movieScrollView;

@end

@implementation MovieDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.currentIndex = -1;
    
    self.tableView.backgroundColor = [Common colorWithRGB:0xffffff];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //    [self setEnableRefresh:YES];
    self.pullTableViewDelegate = self;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.f)];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.f)];
    self.title = @"电影排期";
    [self getMovieDetail];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - Protocol request
- (void)getMovieDetail
{
    [[ProtocolManager shareInstance] getStoreDetail:self.storeId sendType:@"电影院" target:self selector:@selector(getMovieDetailCallback:)];
}

#pragma mark - Protocol call back
- (void)getMovieDetailCallback:(NSDictionary*)dic
{
    NSLog(@"getMovieDetailCallback %@", dic);
    if (dic == nil) {
        [self resetPullTableViewState];
        return;
    }
    if (self.currentIndex == -1) {
        self.ticketList = [NSMutableArray array];
    }
    NSArray* ticketsList = [Common valueForKey:P_TICKETS dic:dic];
    NSDictionary* cinemaInfo = [Common valueForKey:P_CINEMAINFO dic:dic];
    NSArray* movieInfo = [Common valueForKey:P_MOVIEINFOS dic:dic];
    self.cinemaInfo = cinemaInfo;
    if ([ticketsList isKindOfClass:[NSArray class]]) {
        [self.ticketList addObjectsFromArray:ticketsList];
        self.currentIndex += self.ticketList.count;
    }
    if ([movieInfo isKindOfClass:[NSArray class]]) {
        self.movieInfo = movieInfo;
    }
    self.movieDetail = _movieInfo[0];
    if (_movieDetail[P_TODAY] != [NSNull null]) {
        self.movieTimeList  = [NSMutableArray arrayWithArray:_movieDetail[P_TODAY]];
    }
    //self.movieTimeList = [Common valueForKey:P_TODAY dic:_movieDetail];
    [self.tableView reloadData];
}

#pragma mark - Private
//加载cell数据
//加载电影院头部的信息
- (void)loadHeadCellData:(UITableViewCell*)cell index:(int)index
{
    if (self.cinemaInfo == nil) {
        return;
    }
    NSDictionary* cellData = self.cinemaInfo;
    NSString *name = [Common valueForKey:P_NAME dic:cellData];
    UILabel *cinemaName = (UILabel*)[cell viewWithTag:1001];
    cinemaName.text = name;
    NSString *address = [Common valueForKey:P_ADRESS dic:cellData];
    UILabel *cinemaAddress = (UILabel*)[cell viewWithTag:1004];
    cinemaAddress.text = address;
    CGFloat star = [[Common valueForKey:P_RATE dic:cellData] floatValue]/2;
    UIImageView *img = (UIImageView*)[cell viewWithTag:1005];
    img.image = [UIImage imageNamed:@"where/more1@2x.png"];
    [self setupStarViewForCell:cell star:star];
}

//加载电影票的信息
- (void)loadTicketCellData:(UITableViewCell*)cell index:(int)index
{
    if (index >= self.ticketList.count) {
        return;
    }
    NSDictionary* cellData = self.ticketList[index];
    NSString *name = [Common valueForKey:P_NAME dic:cellData];
    NSString *price = [Common valueForKey:P_PRICE dic:cellData];
    UILabel *lbName = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, 30, 30)];
    NSString *ticket = [NSString stringWithFormat:@"%@元,%@",price,name];
    UIFont *font = [UIFont fontWithName:@"Helvetica" size:16.0];
    [lbName setFont:font];
    [lbName setNumberOfLines:0];
    [lbName setBackgroundColor:[UIColor clearColor]];
    CGSize size = [ticket sizeWithFont:font constrainedToSize:CGSizeMake(230.0f, 200.0f) lineBreakMode:UILineBreakModeWordWrap];
    CGRect rect = lbName.frame;
    rect.size = size;
    [lbName setFrame:rect];
    [lbName setText:ticket];
    [cell addSubview:lbName];
    
    UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(cell.bounds.size.width-80, 10, 70, 25)];
    buttonView.layer.cornerRadius = 3;
    buttonView.backgroundColor = [UIColor orangeColor];
    [cell addSubview:buttonView];
    
    UILabel *button = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 70, 25)];
    button.textColor = [UIColor whiteColor];
    button.textAlignment = NSTextAlignmentCenter;
    button.font = [UIFont fontWithName:@"TrebuchetMS" size:15.0];
    button.text = @"购买通票";
    [buttonView addSubview:button];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(10, MOVIETICKETCELL_HEIGHT, 300, 1)];
    lineView.backgroundColor = [Common colorWithRGB:0xDFDFDF];
    [cell addSubview:lineView];
}

//加载中间电影
//设置scrollview
- (void)setupScrollViewCell:(UITableViewCell*)cell
{
    if (self.movieInfo == nil) {
        UIView *noMovie = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, MOVIELISTCELL_HEIGHT)];
        noMovie.backgroundColor = [Common colorWithRGB:0xDFDFDF];
        UILabel *noMoiveLabel = [[UILabel alloc] initWithFrame:CGRectMake(cell.bounds.size.width/2-50, cell.bounds.size.height/2-10, 150, 20)];
        noMoiveLabel.textColor = [UIColor grayColor];
        noMoiveLabel.text = @"暂无排片信息";
        [noMovie addSubview:noMoiveLabel];
        noMovie.tag = 11000;
        [cell addSubview:noMovie];
        return;
    }
    [[cell viewWithTag:11000] removeFromSuperview];
    UIScrollView *MovieScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, MOVIELISTCELL_HEIGHT)];
    [self scrollVIewCellConfig:MovieScroll];
    self.movieScrollView = MovieScroll;
    int originX = 0;
    int movieId = 0;
    for(NSDictionary *movie in self.movieInfo)
    {
        //获取字段数据
        NSString *imgUrl = [Common valueForKey:P_PHOTO dic:movie];
        //view的点击事件回调
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapMovieTimeView:)];
        //构图
        UIView *movieView = [[UIView alloc] initWithFrame:CGRectMake(originX, 10, 130, 170)];
        movieView.tag = movieId+1200;
        UIImageView *scrollImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 0, 120, 150)];
        [scrollImage setImageWithURL:[Common getFullDownloadPathByRelativePath:imgUrl]];
        [movieView addGestureRecognizer:tapGesture];
        [movieView addSubview:scrollImage];
        [MovieScroll addSubview:movieView];
        originX += 130;
        movieId++;
    }
    [MovieScroll setContentSize:CGSizeMake(originX+190, MOVIELISTCELL_HEIGHT-10)];
    [cell.contentView addSubview:MovieScroll];
    
    //小三角形view
    UIImageView *triangle = [[UIImageView alloc]initWithFrame:CGRectMake(55, cell.bounds.size.height-10, 20, 10)];
    triangle.image = [UIImage imageNamed:@"movie/triangle.png"];
    triangle.backgroundColor = [UIColor clearColor];
    [cell addSubview:triangle];
    
}

- (void)scrollVIewCellConfig:(UIScrollView*)MovieScroll
{
    //设置委托
    MovieScroll.delegate = self;
    //设置背景颜色
    MovieScroll.backgroundColor = [Common colorWithRGB:0xDFDFDF];
    //设置取消触摸
    MovieScroll.canCancelContentTouches = NO;
    //设置滚动条类型
    MovieScroll.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    //是否自动裁切超出部分
    MovieScroll.clipsToBounds = YES;
    //控制控件是否能滚动
    MovieScroll.scrollEnabled = YES;
    //设置是否可以进行画面切换
    MovieScroll.pagingEnabled = NO;
    //设置在拖拽的时候是否锁定其在水平或者垂直的方向
    MovieScroll.directionalLockEnabled = YES;
    //隐藏滚动条设置（水平、跟垂直方向）
    MovieScroll.alwaysBounceHorizontal = NO;
    MovieScroll.alwaysBounceVertical = NO;
    MovieScroll.showsHorizontalScrollIndicator = NO;
    MovieScroll.showsVerticalScrollIndicator = NO;
    
    MovieScroll.canCancelContentTouches = YES;
    MovieScroll.delaysContentTouches = YES;
}

//加载电影名称和评分
- (void)loadMovieNameCellData:(UITableViewCell*)cell index:(int)index
{
    if (index >= self.movieInfo.count) {
        return;
    }
    [[cell viewWithTag:10000] removeFromSuperview];
    NSDictionary *cellData = self.movieDetail;
    NSString *movieName = [Common valueForKey:P_NAME dic:cellData];
    NSString *movieKind = [Common valueForKey:P_MOVIE_KIND dic:cellData];
    NSString *movieRating = [Common valueForKey:P_MOVIE_RATING dic:cellData];
    self.detailId = [[Common valueForKey:P_MOVIE_ID dic:cellData] integerValue];
    
    UIView *movieNameAndRatingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height)];
    movieNameAndRatingView.tag = 10000;
    if (index == 0) {
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, 100, 50)];
        UIFont *font = [UIFont fontWithName:@"Helvetica" size:18.0];
        [nameLabel setFont:font];
        [nameLabel setNumberOfLines:0];
        [nameLabel setBackgroundColor:[UIColor clearColor]];
        CGSize size = [movieName sizeWithFont:font constrainedToSize:CGSizeMake(200.0f, 200.0f) lineBreakMode:UILineBreakModeWordWrap];
        CGRect rect = nameLabel.frame;
        rect.size = size;
        [nameLabel setFrame:rect];
        [nameLabel setText:movieName];
        [movieNameAndRatingView addSubview:nameLabel];
        
        int temp = rect.size.width;
        NSArray *movieKindArray = [movieKind componentsSeparatedByString:@","];
        for(NSString *movieKindItem in movieKindArray)
        {
            UIImageView *type = nil;
            if ([movieKindItem isEqualToString:@"3D"]) {
                type = [[UIImageView alloc] initWithFrame:CGRectMake(temp+10, 15, 20, 20)];
                type.image = [UIImage imageNamed:@"movie/movie_3d.png"];
                temp = temp + 20;
            }
            else if ([movieKindItem isEqualToString:@"IMAX3D"]){
                type = [[UIImageView alloc] initWithFrame:CGRectMake(temp+15, 15, 40, 20)];
                type.image = [UIImage imageNamed:@"movie/movie_imax3d.png"];
                temp = temp + 40;
            }
            else if ([movieKindItem isEqualToString:@"IMAX"]){
                type = [[UIImageView alloc] initWithFrame:CGRectMake(temp+15, 15, 40, 20)];
                type.image = [UIImage imageNamed:@"movie/movie_imax2d.png"];
                temp = temp + 40;
            }
            else{
                break;
            }
            [movieNameAndRatingView addSubview:type];
        }
        
        UILabel *ratingLabel = [[UILabel alloc] initWithFrame:CGRectMake(temp + 20, 20, 60, 10)];
        ratingLabel.font = [UIFont fontWithName:@"Helvetica" size:15.0];
        ratingLabel.textColor = [Common colorWithRGB:0xd3b322];
        ratingLabel.text = [NSString stringWithFormat:@"%@分", movieRating];
        if(movieRating == nil){
            ratingLabel.text = @"无评分";
        }
        [movieNameAndRatingView addSubview:ratingLabel];
        
        UIImageView *push = [[UIImageView alloc] initWithFrame:CGRectMake(cell.bounds.size.width-20, 15, 10, 20)];
        push.image = [UIImage imageNamed:@"where/more1@2x.png"];
        [movieNameAndRatingView addSubview:push];
    }
    else {
        NSArray *day = [self getThreeDays];
        int dayX = 10;
        for(int i = 0;i < 3; i++){
            UIView *dateView = [[UIView alloc] initWithFrame:CGRectMake( dayX, 7, 100, 30)];
            UITapGestureRecognizer *tapDate = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapChangeDateView:)];
            dateView.tag = 1301 + i;
            dateView.layer.cornerRadius = 3;
            dateView.backgroundColor = [UIColor orangeColor];
            NSArray *dayArray = [day[i] componentsSeparatedByString:@"-"];
            UILabel *date = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
            date.tag = 1401 + i;
            NSString *format = @"今日%@月%@日";
            date.textColor = [UIColor whiteColor];
            date.textAlignment = NSTextAlignmentCenter;
            date.font = [UIFont fontWithName:@"Helvetica" size:15.0];
            if (i == 1) {
                format = @"明天%@月%@日";
                dateView.backgroundColor = [UIColor clearColor];
                date.textColor = [UIColor blackColor];
                self.Tomorrow = dateView;
            } else if (i == 2) {
                format = @"后天%@月%@日";
                dateView.backgroundColor = [UIColor clearColor];
                date.textColor = [UIColor blackColor];
                self.AfterTomorrow = dateView;
            } else {
                self.Today = dateView;
            }
            date.text = [NSString stringWithFormat:format,dayArray[0],dayArray[1]];
            [dateView addGestureRecognizer:tapDate];
            [dateView addSubview:date];
            [movieNameAndRatingView addSubview:dateView];
            dayX = dayX + 100;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    [cell.contentView addSubview:movieNameAndRatingView];
}

//加载电影时间表
- (void)loadShowTimeCellData:(UITableViewCell*)cell index:(int)index
{
    if (index >= self.movieTimeList.count) {
        return;
    }
    NSDictionary *cellData = self.movieTimeList[index];
    cell.backgroundColor = [Common colorWithRGB:0xeaeaea];
    NSString *beginTime = [Common valueForKey:P_BEGIN_TIME dic:cellData];
    NSString *endTime = [Common valueForKey:P_END_TIME dic:cellData];
    NSString *hallType = [Common valueForKey:P_HALL_TYPE dic:cellData];
    NSString *hallNo = [Common valueForKey:P_HAlL_NO dic:cellData];

    UILabel *beginTimeLabel = (UILabel*)[cell viewWithTag:1101];
    beginTimeLabel.text = beginTime;
    UILabel *hallTypeLabel = (UILabel*)[cell viewWithTag:1102];
    hallTypeLabel.text = hallType;
    UILabel *endTimeLabel = (UILabel*)[cell viewWithTag:1103];
    endTimeLabel.text = [NSString stringWithFormat:@"%@ 结束", endTime];
    UILabel *hallNoLabel = (UILabel*)[cell viewWithTag:1104];
    //hallNoLabel.text = [NSString stringWithFormat:@"%@号厅", @"6"];
    hallNoLabel.text = hallNo;
    
    UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(cell.bounds.size.width-65, 25, 50, 25)];
    buttonView.layer.cornerRadius = 3;
    buttonView.backgroundColor = [UIColor orangeColor];
    [cell addSubview:buttonView];
    
    UILabel *button = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 25)];
    button.textColor = [UIColor whiteColor];
    button.textAlignment = NSTextAlignmentCenter;
    button.font = [UIFont fontWithName:@"Helvetica" size:15.0];
    button.text = @"分享";
    [buttonView addSubview:button];
    
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, MOVIESHOWTIME_HEIGHT, 320, 1)];
    lineView.backgroundColor = [Common colorWithRGB:0xDFDFDF];
    [cell addSubview:lineView];
}

//设置星级
- (void)setupStarViewForCell:(UITableViewCell*)cell star:(CGFloat)star
{
    UIView *starView = [cell viewWithTag:1002];
    for (UIView* view in starView.subviews) {
        [view removeFromSuperview];
    }
    
    HotRateView *hotView = [[HotRateView alloc] initWithHotRate:star];
    CGRect frame = hotView.frame;
    frame.origin.x = 0;
    frame.origin.y = (starView.bounds.size.height - hotView.bounds.size.height)/2;
    hotView.frame = frame;
    [starView addSubview:hotView];
}

#pragma mark - Table view data source
//返回table中有多少个组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 5;
}

//返回table中一个组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ((section == 0) || (section == 2)) {
        return 1;
    }
    else if (section == 1){
        return self.ticketList.count;
    }
    else if (section == 3){
        return 2;
    }
    else if (section == 4){
        return self.movieTimeList.count;
    }
    return self.ticketList.count;
}

//用cellForRowAtIndexPath来得到一个包含每一行显示信息的UITableViewCell对象.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    switch (indexPath.section) {
        case 0:
        {
            
            cell = [tableView dequeueReusableCellWithIdentifier:@"MovieDetailListCell"];
            // Configure the cell...
            if(cell == nil)
            {
                cell = [[NSBundle mainBundle] loadNibNamed:@"MovieDetailListCell"  owner:self options:nil][0];
            }
            [self loadHeadCellData:cell index:indexPath.row];
            //cell.selectionStyle = UITableViewCellSelectionStyleNone;
            break;
        }
        case 1:
        {
            cell = [tableView dequeueReusableCellWithIdentifier:@"TicketCell"];
            // Configure the cell...
            if(cell == nil)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TicketCell"];
            }
           [self loadTicketCellData:cell index:indexPath.row];
            break;
        }
        case 2:
        {
            cell = [tableView dequeueReusableCellWithIdentifier:@"MiddleCell"];
            // Configure the cell...
            if(cell == nil)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MiddleCell"];
            }
            [self setupScrollViewCell:cell];
            break;
        }
        case 3:
        {
            cell = [tableView dequeueReusableCellWithIdentifier:@"MovieNameCell"];
            // Configure the cell...
            if(cell == nil)
            {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MovieNameCell"] autorelease];
            }
            [self loadMovieNameCellData:cell index:indexPath.row];
            break;
        }
        case 4:
        {
            cell = [tableView dequeueReusableCellWithIdentifier:@"MovieTimeCell"];
            // Configure the cell...
            if(cell == nil)
            {
                                cell = [[NSBundle mainBundle] loadNibNamed:@"MovieDetailListCell"  owner:self options:nil][1];
            }
            [self loadShowTimeCellData:cell index:indexPath.row];
            break;
        }
        default:
        {
            cell = [tableView dequeueReusableCellWithIdentifier:@"dddCell"];
            // Configure the cell...
            if(cell == nil)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"dddCell"];
            }
        }
    }
    //    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

//每一个cell定义不同的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0) {
        return TITLECELL_HEIGHT;
    }
    else if (indexPath.section == 1) {
        return MOVIETICKETCELL_HEIGHT;
    }
    else if (indexPath.section == 2) {
        return MIDDLEMOVIECELL_HEIGHT;
    }
    else if (indexPath.section == 3) {
        return MOVIENAMECELL;
    }
    else if (indexPath.section == 4) {
        return MOVIESHOWTIME_HEIGHT;
    }
    else{
        return 0;
    }
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        CinemaDetailViewController *cinemaDetailViewCtrl = [[CinemaDetailViewController alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        cinemaDetailViewCtrl.storeId = [[Common valueForKey:P_ID dic:self.cinemaInfo] integerValue];
        [self.navigationController pushViewController:cinemaDetailViewCtrl animated:YES];
    }
    else if (indexPath.section == 1){
        if (indexPath.row >= self.ticketList.count) {
            return;
        }
        
        NSDictionary* dic = self.ticketList[indexPath.row];
        GoodDetailViewController *goodDetailViewCtrl = [[GoodDetailViewController alloc] init];
        goodDetailViewCtrl.goodId = [[Common valueForKey:P_ID dic:dic] integerValue];
        goodDetailViewCtrl.goodsInfo = dic;
        goodDetailViewCtrl.storeInfo = self.cinemaInfo;
        [self.navigationController pushViewController:goodDetailViewCtrl animated:YES];
        
    }
    else if (indexPath.section == 3)
    {
        if(indexPath.row == 0){
            SingleMovieDetailViewController *singleMovieDetailViewCtrl = [[SingleMovieDetailViewController alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
            singleMovieDetailViewCtrl.goodId = self.detailId;
            [self.navigationController pushViewController:singleMovieDetailViewCtrl animated:YES];
        }
    }
}

//点击事件
- (void)onTapMovieTimeView:(UITapGestureRecognizer*)gesture
{
    NSLog(@"onTapMovieView %d", gesture.view.tag);
    [self.movieScrollView setContentOffset:CGPointMake((gesture.view.tag-1200) * 130.0, 0) animated:YES];
    if (gesture.view.tag != self.updateTime) {
        [self reloadName:gesture.view.tag];
        self.updateTime = gesture.view.tag;
    }
}

- (void)onTapChangeDateView:(UITapGestureRecognizer*)Date
{
    NSLog(@"onTapChangeDateView %@",Date.view);
    self.Today.backgroundColor = [UIColor whiteColor];
    self.Tomorrow.backgroundColor = [UIColor whiteColor];
    self.AfterTomorrow.backgroundColor = [UIColor whiteColor];
    Date.view.backgroundColor = [UIColor orangeColor];
    UILabel *date = (UILabel*)[_Today viewWithTag:_Today.tag+100];
    date.textColor = [UIColor blackColor];
    date = (UILabel*)[_Tomorrow viewWithTag:_Tomorrow.tag+100];
    date.textColor = [UIColor blackColor];
    date = (UILabel*)[_AfterTomorrow viewWithTag:_AfterTomorrow.tag+100];
    date.textColor = [UIColor blackColor];
    date = (UILabel*)[Date.view viewWithTag:Date.view.tag+100];
    date.textColor = [UIColor whiteColor];
    
    if ([Date.view isEqual:self.Today]) {
        [self reloadTime:P_TODAY];
    }
    else if ([Date.view isEqual:self.Tomorrow]){
        [self reloadTime:P_TOMORROW];
    }else if ([Date.view isEqual:self.AfterTomorrow]){
        [self reloadTime:P_AFTER_TOMORROW];
    }
}

//刷新
- (void)reloadName:(int)tag
{
    //重载name和rating
    self.currentId = tag - 1200;
    self.movieDetail = _movieInfo[_currentId];
    NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:3];
    [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
    [self reloadTime:P_TODAY];
}

- (void)reloadTime:(NSString*)date
{
    //重载时间列表
    [self.tableView beginUpdates];
    [self.movieTimeList removeAllObjects];
    NSArray* movieTimeList = [Common valueForKey:date dic:_movieDetail];
    [self.movieTimeList addObjectsFromArray:movieTimeList];
    NSIndexSet *timeListSet=[[NSIndexSet alloc]initWithIndex:4];
    [self.tableView reloadSections:timeListSet withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];
}

//scrollView自带的滑动代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(self.movieScrollView == scrollView){
    float percent = scrollView.contentOffset.x;
    //float yMove = scrollView.contentOffset.y;
    int viewFront = percent / 130.0 + 1.0;
    int tag = viewFront - 1 + 1200;
    UIView *test = (UIView*)[scrollView viewWithTag:tag];
    float temp = test.frame.origin.x;
//    NSLog(@"fuck scroll %f, %f ,%d",percent,temp,viewFront);
    //if( yMove == 0){
        if((percent>=0.0 && percent<10.0) || percent >= 130){
            if (self.updateTime != tag) {
                if ((percent >= temp-5.0&&percent <= temp+10.0)) {
                    [self reloadName:tag];
                    self.updateTime = tag;
                }
            }
        }
    }
}

//获取今天、明天、后天
- (NSArray*)getThreeDays
{
    NSDate *now = [[NSDate alloc] init];
    NSTimeInterval secondsPerDay = 24 * 60 * 60;
    //明天时间
    NSDate *tomorrow = [[NSDate alloc] initWithTimeIntervalSinceNow:secondsPerDay];
    //昨天时间
    NSDate *afterTomorrow = [[NSDate alloc] initWithTimeIntervalSinceNow:secondsPerDay*2];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"MM-dd"];
    NSString *todayS = [dateformatter stringFromDate:now];
    NSString *tomorrowS = [dateformatter stringFromDate:tomorrow];
    NSString *afterTomorrowS = [dateformatter stringFromDate:afterTomorrow];
    NSArray *threeDays = [NSArray arrayWithObjects:todayS,tomorrowS,afterTomorrowS, nil];
    return threeDays;
}

#pragma mark - PullTableViewDelegate
- (void)pullTableViewDidTriggerRefresh:(PullTableView*)pullTableView
{
    NSLog(@"refresh...");
    self.currentIndex = -1;
    [self getMovieDetail];
}

-(void)pullTableViewDidTriggerLoadMore:(PullTableView *)pullTableView
{
    NSLog(@"loadMore...");
    [self getMovieDetail];
}

- (void)resetPullTableViewState
{
    [self setPullTableIsRefreshing:NO];
    [self setPullTableIsLoadMore:NO];
}


@end
